﻿using DNFImagePacks2Manager.Helpers;
using System.Collections.Generic;
using System.IO;

namespace DNFImagePacks2Manager.NPK
{
    public class ImageDDSFile : ImageFile
    {
        public bool IsCompress { get; set; }
        public int ImageWidth { get; set; }
        public int ImageHeight { get; set; }
        public int Temp1 { get; set; }
        public int PositionX { get; set; }
        public int PositionY { get; set; }
        public int FrameWidth { get; set; }
        public int FrameHeight { get; set; }
        public int Temp2 { get; set; }
        public int DDSIndex { get; set; }
        public int CutLeft { get; set; }
        public int CutTop { get; set; }
        public int CutRight { get; set; }
        public int CutBottom { get; set; }
        public int Temp3 { get; set; }

        public ImageDDSFile(BinaryReader reader, ImageType type) : base(type)
        {
            byte[] compressBytes = reader.ReadBytes(4);
            byte[] imageWidthBytes = reader.ReadBytes(4);
            byte[] imageHeightBytes = reader.ReadBytes(4);
            byte[] temp1Bytes = reader.ReadBytes(4);
            byte[] positionXBytes = reader.ReadBytes(4);
            byte[] positionYBytes = reader.ReadBytes(4);
            byte[] frameWidthBytes = reader.ReadBytes(4);
            byte[] frameHeightBytes = reader.ReadBytes(4);
            byte[] temp2Bytes = reader.ReadBytes(4);
            byte[] ddsIndexBytes = reader.ReadBytes(4);
            byte[] cutLeftBytes = reader.ReadBytes(4);
            byte[] cutTopBytes = reader.ReadBytes(4);
            byte[] cutRightBytes = reader.ReadBytes(4);
            byte[] cutBottomBytes = reader.ReadBytes(4);
            byte[] temp3Bytes = reader.ReadBytes(4);

            IsCompress = ByteHelper.Bytes2Int(compressBytes) == 0x07;
            ImageWidth = ByteHelper.Bytes2Int(imageWidthBytes);
            ImageHeight = ByteHelper.Bytes2Int(imageHeightBytes);
            Temp1 = ByteHelper.Bytes2Int(temp1Bytes);
            PositionX = ByteHelper.Bytes2Int(positionXBytes);
            PositionY = ByteHelper.Bytes2Int(positionYBytes);
            FrameWidth = ByteHelper.Bytes2Int(frameWidthBytes);
            FrameHeight = ByteHelper.Bytes2Int(frameHeightBytes);
            Temp2 = ByteHelper.Bytes2Int(temp2Bytes);
            DDSIndex = ByteHelper.Bytes2Int(ddsIndexBytes);
            CutLeft = ByteHelper.Bytes2Int(cutLeftBytes);
            CutTop = ByteHelper.Bytes2Int(cutTopBytes);
            CutRight = ByteHelper.Bytes2Int(cutRightBytes);
            CutBottom = ByteHelper.Bytes2Int(cutBottomBytes);
            Temp3 = ByteHelper.Bytes2Int(temp3Bytes);
        }

        public override byte[] GetIndexBytes()
        {
            List<byte> bytes = [];
            bytes.AddRange(Type.GetBytes());
            bytes.AddRange(ByteHelper.IntList2Bytes([
                GetCompressCode(),
                ImageWidth,
                FrameHeight,
                Temp1,
                PositionX,
                PositionY,
                FrameWidth,
                FrameHeight,
                Temp2,
                DDSIndex,
                CutLeft,
                CutTop,
                CutRight,
                CutBottom,
                Temp3
                ]));
            return [.. bytes];
        }

        public static int GetCompressCode(bool isCompress)
        {
            if (isCompress)
            {
                return 0x07;
            }
            else
            {
                return 0x05;
            }
        }

        public int GetCompressCode()
        {
            return GetCompressCode(IsCompress);
        }

        public override int GetSize()
        {
            return 0;
        }

        public override bool IsCompressed()
        {
            return IsCompress;
        }
    }
}
